var iframeList = new Array();
var removeTabLockTabId = "";


$(function(){
	var height = $(document.body).height() - 46;
    $('#leftmenu').height(height);
    $('.mainbody').height(height);
	initLeftPanel();
	
	iframeList.push($("#首页")) 
 

});

String.prototype.startWith=function(str){
	if(str==null||str==""||this.length==0||str.length>this.length)
	  return false;
	if(this.substr(0,str.length)==str)
	  return true;
	else
	  return false;
	return true;
}

function initLeftPanel(){
	$(".para-title2").each(function(){
		var id = $(this).attr("id");
		var no = id.substr('para-title'.length , id.length);
		$(this).click(function(){
			 $("#lv_box"+no).fadeToggle(200);
			 //fadeOutOtherLvBox(no);
		});
	}); 
}

function fadeOutOtherLvBox(no){
	//其他父类别
	var boxId = 'lv_box'+no;
	$('.lv_box').each(function(){
		var id = $(this).attr("id");
		if(!boxId.startWith(id)){
			console.log(id );
			$('#'+id).fadeOut();
		} 
	});
	$('.lv_box2').each(function(){
		var id = $(this).attr("id");
		if(boxId!=id){
			$('#'+id).fadeOut();
		} 
	});
}

function triggerPage(dom){
	framePageGo($(dom).data('src'),$(dom).find('span').html());
}
function triggerOpen(dom){
    window.open($(dom).data('src'));
}


function getIframeById(tabId){
	var iframe = null;
    $('#currentIframe iframe').each(function () {
		if($(this).attr('id')==tabId){
			console.log($(this).attr('id'));
            iframe = $(this);
		}
    });
    return iframe;
}

function framePageGo(url,tabId){
	if(getIframeById(tabId)==null){
		//激活
		var newIframe = '<iframe id="'+tabId+'" src="'+url+'"  frameborder="no" width="100%" height="100%" scrolling="yes" allowtransparency="true" border="0" style="background-color:white"></iframe>';
		$('#currentIframe').append(newIframe);
		iframeList.push($("#"+tabId));

		$('#tab').append('<li   data-id="'+tabId+'" style="width:116px" class="clickmenu" onclick="tabActive(this)"><div   style="width:100px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis;float: left">'+tabId+'</div><div class="deletbt" onclick="tabClose(this)" ></div></li>');
	} 
	   
	activePage(tabId);
}
function tabClose(dom){
	
	var tabId = $(dom).parent().data('id');
	
	var nextDom =getNextTab(tabId);
	
	if(nextDom!=""){
		activePage(nextDom.data('id'));
	}
	//删除掉 iframe 和 tab标签.
	removePage(tabId);
	//然后激活下一个
}
function removePage(tabId){
	removeIframe(tabId);
	removeTab(tabId);
}
function removeTab(tabId){
	removeTabLockTabId = tabId;
	$("#tab").find('[data-id="'+tabId+'"]').remove();
}

function tabActive(dom){
	if($(dom).data('id')==removeTabLockTabId){
		removeTabLockTabId = "";
		return ;
	}
	activePage($(dom).data('id'));
}


function activePage(tabId){
	//限制tab 标签的class
	triggerTabClass(tabId);
	//显示对应的iframe
	showIframe(tabId);
	if(tabId!='首页'){
		showItemClass(tabId);	
	} 
}

function showItemClass(tabId){
	$('#leftmenu .menu-bg').each(function(){
		if($(this).data('id') == tabId){
			$(this).find('span').addClass("clickleft");
		}else{
			$(this).find('span').removeClass("clickleft");
		}
	});
}

function removeIframe(tabId){
	var newIframeList = new Array();
	for(var i =0 ;i<iframeList.length;i++){
		var iframe = iframeList[i]; 
		if(iframe.attr('id')!=tabId){
			newIframeList.push(iframe);
		}
	}
    getIframeById(tabId).remove();
	iframeList = newIframeList;
}

function showIframe(tabId){
	for(var i =0 ;i<iframeList.length;i++){
		var iframe = iframeList[i];
		if(iframe.attr('id')==tabId){
			iframe.show();
		}else{
			iframe.hide();
		}
	}
}

function triggerTabClass(tabId){
		 $("#tab li").each(function(){ 
	        	if($(this).data("id") == tabId){
				    $(this).addClass("clickmenu");
	        	}else{
	        		$(this).removeClass("clickmenu");
	        	}
	     }); 
} 

function removeLeftPanelClass(itemName){
		 $(".lv_box  span").each(function(){
		 	if($(this).html() != tabName){ 
				    current.removeClass("clickleft");
	        }  
		 });
}
 
 

function getNextTab(tabId){
	var tabLength = $("#tab li").length;
    var next = "";
    var nextDom = "";
    
    var preIndex = 0;
    var nowIndex = 0;
    var afterIndex = 0 ;
    
    for(var i = 0;i<tabLength;i++){
        var dom = $($("#tab li")[i]);
        if(dom.html() == "首页"){continue;}
        if(dom.data("id") == tabId){
        	nowIndex = i;
        	break;
        }
    } 
    if(nowIndex>=tabLength-1){
    	 nowIndex--;
    }else{
    	 nowIndex++;
    }
    return $($("#tab li")[nowIndex]);
}

function activeMainPage(){
	activePage('首页');
}

function activeNewPage(setting){
	var tabId = setting.tabName;
	var srcUrl = setting.srcUrl;
	var out = setting.out;
	if(out!=undefined && out!=""){
		removePage(out);
	}
	framePageGo(srcUrl,tabId);
}
